package com.hnucm.cooklife.presenter.order;

import com.hnucm.cooklife.base.BasePresenter;
import com.hnucm.cooklife.bean.db.OrderSec;
import com.hnucm.cooklife.contract.order.Contract;
import com.hnucm.cooklife.model.OrderModel;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class OrderPresenter extends BasePresenter<Contract.IOrderView> implements Contract.IOrderPresenter {
    Contract.IOrderModel mIOrderModel;

    public OrderPresenter() {
        mIOrderModel = new OrderModel();
    }

    @Override
    public void loadOrder() {
        mIOrderModel.loadOrder()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<OrderSec>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(@NonNull List<OrderSec> orderSecs) {
                        if(isViewAttached()){
                            getView().onLoadOrder(orderSecs);
//                            System.out.println(orderSecs.get(0).orderpicList.get(0).pic);
//                            dto.getOrderpicList().get(0).pic
                            getView().onLoadSuccess();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        e.printStackTrace();
                        if (isViewAttached()) {
                            getView().onLoadingFailed();
                            System.out.println(e.getMessage());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void refreshOrder() {

    }
}
